package com.csyf.manager.service.impl;

import com.csyf.manager.common.CommonConst;
import com.csyf.manager.dao.SysPayProductMapper;
import com.csyf.manager.entity.SysPayProduct;
import com.csyf.manager.service.SysProductService;
import com.csyf.manager.utils.page.JqGridVo;
import com.csyf.manager.utils.page.PageUtils;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: casper
 * @Description: 产品ServiceImpl
 * @Date: Created in 2018/12/11 15:53
 */
@Slf4j
@Service
public class SysProductServiceImpl implements SysProductService {

    @Autowired
    private SysPayProductMapper sysPayProductMapper;

    /**
     * 产品列表
     * @param vo
     * @param page
     * @param rows
     * @return
     */
    @Override
    public JqGridVo queryProudctList(SysPayProduct vo, Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        PageHelper.orderBy("create_time desc");

        Example example = new Example(SysPayProduct.class);
        Example.Criteria criteria = example.createCriteria();
        PageUtils.andEqualTo(criteria, "prodCode", vo.getProdCode());
        PageUtils.andEqualTo(criteria, "prodStatus", vo.getProdStatus());

        List<SysPayProduct> select = sysPayProductMapper.selectByExample(example);
        return PageUtils.pasrJqGrid(select);
    }

    /**
     * 新增产品
     * @param productForm
     */
    @Override
    public void saveProduct(SysPayProduct productForm) {
        productForm.setCreateTime(new Date());
        productForm.setModifyTime(new Date());
        productForm.setProdStatus(CommonConst.STATUS_VALID);
        sysPayProductMapper.insertSelective(productForm);
    }

    /**
     * 根据ID查询产品
     * @param productId
     * @return
     */
    @Override
    public SysPayProduct queryProductById(Long productId) {
        return sysPayProductMapper.selectByPrimaryKey(productId);
    }

    /**
     * 根据代理商id更新产品信息
     * @param productForm
     * @return
     */
    @Override
    public int updateByProductId(SysPayProduct productForm) {
        return sysPayProductMapper.updateByProductId(productForm);
    }

    /**
     * 根据产品id集合批量更新产品状态
     * @param productIds
     * @param status
     * @return
     */
    @Override
    public int updateStatusBatch(List<String> productIds, Integer status) {
        Map<String,Object> params = new HashMap<>(2);
        params.put("productIds", productIds);
        params.put("prodStatus", status);
        return sysPayProductMapper.updateStatusBatch(params);
    }


}
